package com.yijianonghu.service.wxwebsite.products;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yijianonghu.dal.model.ProAccessHistory;
import com.yijianonghu.dal.model.ProAccessHistoryExample;
import com.yijianonghu.dal.model.SysBlog;
import com.yijianonghu.dal.model.SysBlogExample;
import com.yijianonghu.util.Constants;
import com.yijianonghu.util.DBUtils;
import com.yijianonghu.util.exception.NoResultReturnedException;

import net.javahub.dao.BaseSqlDao;
import net.javahub.dao.IBaseDao;
import net.javahub.dao.BaseSqlDao.SqlWrapper;
import net.javahub.service.BaseTransactionalService;

@Service
public class BaseProductDetailService extends BaseTransactionalService<SysBlog, Integer, SysBlogExample>{
	
	@Autowired
	protected BaseSqlDao baseSqlDao;
	
	@Autowired
	protected IBaseDao<ProAccessHistory, Integer, ProAccessHistoryExample> proAccessHistoryDao;
	
	public List<Map<String, Object>> getDetailInfo(Integer userId, Integer id) throws Exception{
		//商家信息
		SqlWrapper sqlWrapper=new SqlWrapper();
		sqlWrapper.setSqlStatement("select mt.client_id,sc.role from sys_blog mt left join sys_client sc on mt.client_id=sc.id where mt.id=#{params.id} and mt.removeflag=0 and mt.saleflag=1 and mt.type<>0 and mt.include_in_wx=1");
		sqlWrapper.addParam("id", id);
		List<Map<String, Object>> sellerList=baseSqlDao.select(sqlWrapper);
		if(sellerList==null || sellerList.isEmpty() || (sellerList.get(0)==null)){
			throw new NoResultReturnedException("无商家信息！");
		}
		Integer sellerId=DBUtils.parseToNumber(Integer.class, sellerList.get(0).get("client_id"));
		Byte sellerRole=DBUtils.parseToNumber(Byte.class, sellerList.get(0).get("role"));
		
		String sql="";
		if(sellerId==1 || sellerRole==1){
			sql="select c.qq,c.nickname,c.avatar,c.address,c.mobile,c.starscore as farmer_starscore,c.replycount as farmer_replycount,mt.* from sys_blog mt,sys_client c where mt.id=#{params.id} and mt.client_id=c.id and mt.removeflag=0 and mt.include_in_wx=1";
		} else {
			sql="select c.qq,c.nickname,c.avatar,c.address,c.mobile,c.starscore as farmer_starscore,c.replycount as farmer_replycount,mt.* from sys_blog mt, 4_merchant_roles c where mt.id=#{params.id} and mt.role_id=c.id and mt.removeflag=0 and mt.include_in_wx=1";
		}
		
		sqlWrapper.setSqlStatement(sql);
		
		List<Map<String,Object>> baseInfoList=baseSqlDao.select(sqlWrapper);
		if(baseInfoList==null || baseInfoList.isEmpty() || baseInfoList.get(0)==null){
			throw new NoResultReturnedException("无基本信息！");
		}
		if(DBUtils.isEqualInteger(Constants.CommonConstants.INTEGER_ONE, baseInfoList.get(0).get("client_id"))){
			sql="select qq,mobile from sys_config limit 1";
			sqlWrapper.setSqlStatement(sql);
			List<Map<String, Object>> platFormDataList=baseSqlDao.select(sqlWrapper);
			if(platFormDataList!=null && (!platFormDataList.isEmpty()) && platFormDataList.get(0)!=null){
				baseInfoList.get(0).put("qq", platFormDataList.get(0).get("qq"));
				baseInfoList.get(0).put("mobile", platFormDataList.get(0).get("mobile"));
			}
		}
		
		//图片信息
		List<Map<String, Object>> imgItems=null;
		if(baseInfoList.get(0).get("imgcount")!=null){
			Integer imgCount=DBUtils.parseToNumber(Integer.class, baseInfoList.get(0).get("imgcount"));
			if(imgCount>0){
				sql="select * from sys_img where keytype=2 and keyid=#{params.id}";
				sqlWrapper.setSqlStatement(sql);
				imgItems=baseSqlDao.select(sqlWrapper);
			}
		}
		baseInfoList.get(0).put("imgItems", imgItems==null?"":imgItems);
		
		//农货详情
		Object img2countObject=baseInfoList.get(0).get("img2count");
		List<Map<String,Object>> img2Items=null;
		if(img2countObject!=null){
			Integer img2Count=(Integer)img2countObject;
			if(img2Count>0){
				sql="select * from sys_img where keytype=19 and keyid=${params.id}";
				img2Items=baseSqlDao.select(sqlWrapper);
			}
		}
		baseInfoList.get(0).put("img2Items", img2Items==null?"":img2Items);
		
		sql="select count(*) from sys_love where keytype=2 and client_id=#{params.client_id} and keyid=#{params.id}";
		sqlWrapper.addParam("client_id", userId);
		sqlWrapper.setSqlStatement(sql);
		List<Map<String, Object>> collectflagList=baseSqlDao.select(sqlWrapper);
		if(collectflagList!=null && (!collectflagList.isEmpty()) && collectflagList.get(0)!=null){
			baseInfoList.get(0).put("collectflag", collectflagList.get(0));
		}
		if(baseInfoList.get(0).get("collectflag")==null){
			baseInfoList.get(0).put("collectflag", 0);
		}
		
		sql="select count(*) from sys_love where client_id=#{params.client_id} and keytype=3 and keyid=#{params.id}";
		sqlWrapper.setSqlStatement(sql);
		List<Map<String,Object>> goodflagList=baseSqlDao.select(sqlWrapper);
		if(goodflagList!=null && (!goodflagList.isEmpty()) && goodflagList.get(0)!=null){
			baseInfoList.get(0).put("goodflag", goodflagList.get(0));
		}
		if(baseInfoList.get(0).get("goodflag")==null){
			goodflagList.get(0).put("goodflag", 0);
		}
		
		if(baseInfoList.get(0).get("shipping_service_ids")!=null){
			String shippingServiceIds=DBUtils.parseToString(baseInfoList.get(0).get("shipping_service_ids"));
			String[] shippingServiceIdArray=shippingServiceIds.split(",");
			List<String> shippingServiceIdList = Arrays.<String>asList(shippingServiceIdArray);
			sql="select id,name from 4_shipping_service where removeflag=0 order by orderby desc";
			sqlWrapper.setSqlStatement(sql);
			List<Map<String, Object>> shippingServiceItems=baseSqlDao.select(sqlWrapper);
			if(DBUtils.isListNotEmpty(shippingServiceItems)){
				baseInfoList.get(0).put("shippingserviceItems", shippingServiceItems);
				for(Map<String, Object> item:shippingServiceItems){
					item.put("selectflag", shippingServiceIdList.contains(item.get("id"))?1:0);
				}
			}
		} else {
			baseInfoList.get(0).put("shippingserviceItems", "");
		}
		
		sql="select name from 4_blog_tags where id in (#{params.tagids}) and removeflag=0 order by orderby desc";
		sqlWrapper.setSqlStatement(sql);
		sqlWrapper.addParam("tagids", baseInfoList.get(0).get("tagids"));
		List<Map<String, Object>> tagItems=baseSqlDao.select(sqlWrapper);
		baseInfoList.get(0).put("tagItems", tagItems==null?"":tagItems);
		
		List<Map<String, Object>> merchantItems=null;
		if(!DBUtils.isEqualInteger(Constants.CommonConstants.INTEGER_ONE,baseInfoList.get(0).get("type"))){
			sql="select mt.id,mt.client_id,mt.nickname,mt.starscore,mt.starcount,mt.salecontent,mt.mobile from 4_merchant_roles mt where mt.id=#{params.role_id} and mt.validflag=1 limit 1";
			sqlWrapper.setSqlStatement(sql);
			sqlWrapper.addParam("role_id", baseInfoList.get(0).get("role_id"));
			merchantItems=baseSqlDao.select(sqlWrapper);
		}
		baseInfoList.get(0).put("merchantItems", merchantItems==null?"":merchantItems);
		
		Byte keyType=DBUtils.parseToNumber(Byte.class, baseInfoList.get(0).get("type"));
		
		ProAccessHistoryExample proAccessHistoryExample=new ProAccessHistoryExample();
		proAccessHistoryExample.createCriteria()
			.andClientIdIsNotNull()
			.andClientIdEqualTo(userId)
			.andKeytypeIsNotNull()
			.andKeytypeEqualTo(keyType)
			.andKeyidIsNotNull()
			.andKeyidEqualTo(id);
		
		List<ProAccessHistory> proAccessHistoryList=proAccessHistoryDao.selectByExample(ProAccessHistory.class, proAccessHistoryExample);
		
		if(DBUtils.isFirstItemOfListNotEmpty(proAccessHistoryList)){
			ProAccessHistory proAccessHistory=proAccessHistoryList.get(0);
			proAccessHistory.setRegdate(new Date());
			proAccessHistoryDao.updateByPrimaryKey(proAccessHistory);
		} else {
			ProAccessHistory proAccessHistory=new ProAccessHistory();
			proAccessHistory.setClientId(userId);
			proAccessHistory.setKeytype(keyType);
			proAccessHistory.setKeyid(id);
			proAccessHistory.setRegdate(new Date());
			proAccessHistoryDao.insert(proAccessHistory);
		}
		return baseInfoList;
	}
}
